package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDto;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.BusinessException;
import com.sky.exception.extend.ShoppingCartIsEmptyException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.IShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements IShoppingCartService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Override
    public void save(ShoppingCartDto shoppingCartDto) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDto,shoppingCart);
        //添加 userId
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //添加创建时间
        shoppingCart.setCreateTime(LocalDateTime.now());
        //添加数量 默认为1
        shoppingCart.setNumber(1);
        //如果是dishId
        if (shoppingCart.getDishId()!=null){
            //查询其多少钱，什么名字 照片地址
            Dish dish = dishMapper.queryById(shoppingCartDto.getDishId());
            //添加菜品名称
            shoppingCart.setName(dish.getName());
            //添加菜品照片
            shoppingCart.setImage(dish.getImage());
            //添加菜品金额
            shoppingCart.setAmount(dish.getPrice());
        }
        // 那么就是套餐
        if (shoppingCart.getSetmealId()!=null){
            Setmeal setmeal = setmealMapper.getSetmealById(shoppingCartDto.getSetmealId());
            //添加套餐名字
            shoppingCart.setName(setmeal.getName());
            //添加金额
            shoppingCart.setAmount(setmeal.getPrice());
            //添加照片
            shoppingCart.setImage(setmeal.getImage());
        }
        //最后插入数据库
        shoppingCartMapper.insert(shoppingCart);
    }

    @Override
    public List<ShoppingCart> list(Long userId) {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(userId);
        if ((shoppingCarts.isEmpty())){
            throw new ShoppingCartIsEmptyException(MessageConstant.SHOPPING_CART_IS_EMPTY);
        }else {
            return shoppingCarts;
        }
    }

    @Override
    public boolean modify(ShoppingCartDto shoppingCartDto) {
        Long userId=BaseContext.getCurrentId();
        //先进行检查
        if (shoppingCartDto.getSetmealId()!=null){
            ShoppingCart shoppingCart=shoppingCartMapper.queryBySetmealId(shoppingCartDto.getSetmealId(),userId);
            //说明是套餐,那么我们进行查询，如果数据库对应的表为null 那么我们返回false
           if (shoppingCart==null){
               return false;
           }else {
               shoppingCart.setNumber(shoppingCart.getNumber()+1);
               shoppingCart.setAmount(setmealMapper.getSetmealById(shoppingCartDto.getSetmealId())
                       .getPrice()
                       .multiply(new BigDecimal(shoppingCart.getNumber())));
               //然后修改表中的值
               shoppingCartMapper.modify(shoppingCart);
               //最后返回true
               return true;
           }
        }else {
            //那么 dishId不为0
           ShoppingCart shoppingCart=shoppingCartMapper.queryByDishId(shoppingCartDto.getDishId(),userId,shoppingCartDto.getDishFlavor());
           if(shoppingCart==null||shoppingCart.getDishFlavor()!=null&&!shoppingCart.getDishFlavor().
                   equals(shoppingCartDto.getDishFlavor())){
               //如果为空，或者flavor不相等，但不为空
               return false;
           }else {
               //说明相同，同理为null的值也是相同
               shoppingCart.setNumber(shoppingCart.getNumber()+1);
               shoppingCart.setAmount(dishMapper.queryById(shoppingCartDto.getDishId())
                               .getPrice()
                       .multiply(new BigDecimal(shoppingCart.getNumber())));
               //修改表中的值
               shoppingCartMapper.modify(shoppingCart);
               //最后返回true
               return true;
           }
        }
    }

    @Override
    public void sub(ShoppingCartDto shoppingCartDto) {
        Long userId=BaseContext.getCurrentId();
        //先进行判断
      if(shoppingCartDto.getSetmealId()!=null){
          //说明只是setmealId
          Setmeal setmeal = setmealMapper.getSetmealById(shoppingCartDto.getSetmealId());
          ShoppingCart shoppingCart = shoppingCartMapper.queryBySetmealId(shoppingCartDto.getSetmealId(), userId);
          if (shoppingCart.getNumber()==1) {
              //如果再减，那肯定是不对的进行删除
              shoppingCartMapper.deleteById(shoppingCart.getId());
          } else {
              shoppingCart.setNumber(shoppingCart.getNumber()-1);
              shoppingCart.setAmount(setmeal.getPrice().multiply(new BigDecimal(shoppingCart.getNumber())));
              shoppingCartMapper.modify(shoppingCart);
          }
      }else {
          //说明是dishId
          ShoppingCart shoppingCart = shoppingCartMapper.queryByDishId(shoppingCartDto.getDishId(), userId,
                  shoppingCartDto.getDishFlavor());
          if(shoppingCart.getNumber()==1){
              //直接删
              shoppingCartMapper.deleteById(shoppingCart.getId());
          }else {
              shoppingCart.setNumber(shoppingCart.getNumber()-1);
              shoppingCart.setAmount(dishMapper.queryById(shoppingCartDto.getDishId()).getPrice()
                      .multiply(new BigDecimal(shoppingCart.getNumber())));
              shoppingCartMapper.modify(shoppingCart);
          }
      }
    }

    @Override
    public void empty() {
        Long userId=BaseContext.getCurrentId();
        shoppingCartMapper.clean(userId);
    }
}
